package com.tistory.admin.common;

import java.sql.SQLException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;

import com.tistory.admin.adminmgr.auth.model.AdminAuthSession;
import com.tistory.admin.adminmgr.auth.model.AdminMember;
import com.tistory.admin.adminmgr.auth.model.LoginUserInfo;
import com.tistory.commons.utils.SessionUtil;

public class SessionManager {
	


	/**
	 * 세션을 생성한다.
	 * 
	 * @param req
	 * @param sessionKey
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static boolean setSession(HttpServletRequest req, String sessionKey, Object obj) {
		if(obj != null) {
			HttpSession session = req.getSession(true);
			session.setMaxInactiveInterval(60 * 30); // 세션시간(초)
			session.setAttribute(sessionKey, obj);
			return true;
		}
		return false;
	}


	/**
	 * 세션에 담겨있는 세션정보를 돌려준다
	 * 
	 * @param req
	 * @param sessionKey
	 * @return
	 */
	private static Object getSession(HttpServletRequest req, String sessionKey) {
		if(isValidSession(req)) {
			return req.getSession(false).getAttribute(sessionKey);
		}
		return null;
	}


	/**
	 * 세션 유효성 체크
	 * 
	 * @param request
	 * @return boolean
	 */
	public static boolean isValidSession(HttpServletRequest req) {
		return (req != null && req.getSession(false) != null);
	}


	/**
	 * 로그아웃 처리 세션정보 제거
	 * 
	 * @param request
	 * @return boolean
	 */
	public static boolean removeSession(HttpServletRequest req, HttpServletResponse res) {
		if(isValidSession(req)) {
			req.getSession(false).invalidate();
			return true;
		}
		return false;
	}


	public static void removeCookie(HttpServletResponse res, String coName, String domain) {
		// 쿠키삭제
		Cookie cookie = new Cookie(coName, null);
		cookie.setDomain(domain);
		cookie.setPath("/");
		cookie.setMaxAge(0); // 쿠키유지시간(초)[-1:쿠키저장안함(해당 창에서만 사용) , 0:쿠키삭제]
		res.addCookie(cookie);
	}


	/**
	 * HttpSession 을 리턴
	 * 
	 * @param request
	 * @return SessionVO
	 */
	public static HttpSession getHttpSession(HttpServletRequest req) {
		if(isValidSession(req)) {
			return req.getSession(false);
		}
		return null;
	}
	
	/**
	 * 세션 로그아웃 처리.
	 * 
	 * @param request
	 * @param modelAndView
	 * @return ModelAndView
	 */

	public static ModelAndView init(HttpServletRequest request,ModelAndView modelAndView) throws SQLException {
		LoginUserInfo loginInfo = (LoginUserInfo)SessionUtil.getMemberSession(request);
		if (loginInfo == null) { // throw exception
			String  returnUrl = "login/process";
			modelAndView.setViewName(returnUrl);
		}
		return modelAndView;
	}
	
	/**
	 * 세션 로그아웃 처리.
	 * 
	 * @param request
	 * @return 
	 */	
	
	public static void funInit(HttpServletRequest request) throws SQLException {
		// 권한체크
		LoginUserInfo loginInfo = (LoginUserInfo)SessionUtil.getMemberSession(request);
		if (loginInfo == null) { // throw exception
			throw new BaseException("사용자 세션정보가 없습니다.");
		}
	}

	
}
